<template>
  <div id="app">
    <el-container>
      <!-- 顶部导航栏 - 只在前台页面显示 -->
      <el-header v-if="showNavigation" class="header">
        <div class="header-container">
          <el-row type="flex" justify="space-between" align="middle">
            <el-col :xs="8" :sm="6" :md="4">
              <div class="logo" @click="$router.push('/')">
                🍽️ 美食网
              </div>
            </el-col>
            
            <el-col :xs="0" :sm="12" :md="16">
              <el-menu 
                mode="horizontal" 
                :default-active="$route.path"
                class="nav-menu"
                @select="handleMenuSelect"
              >
                <el-menu-item index="/">首页</el-menu-item>
                <el-menu-item index="/popular">热门推荐</el-menu-item>
                <el-menu-item index="/categories">美食分类</el-menu-item>
                <el-menu-item index="/restaurants">美食商家</el-menu-item>
                <el-menu-item index="/about">关于我们</el-menu-item>
              </el-menu>
            </el-col>
            
            <el-col :xs="16" :sm="6" :md="4">
              <div class="header-right">
                <!-- 用户操作区 -->
                <div class="user-actions">
                  <div v-if="!isLoggedIn" class="auth-buttons">
                    <el-button type="primary" @click="$router.push('/login')">登录</el-button>
                    <el-button @click="$router.push('/register')">注册</el-button>
                  </div>
                  <el-dropdown v-else @command="handleUserCommand" class="user-dropdown">
                    <span class="user-info">
                      <el-avatar :size="32" :src="getUserAvatar()" />
                      <span class="username">{{ currentUser.username }}</span>
                      <el-icon><arrow-down /></el-icon>
                    </span>
                    <template #dropdown>
                      <el-dropdown-menu>
                        <el-dropdown-item :command="getDashboardRoute()">
                          <el-icon><User /></el-icon>
                          {{ getDropdownText() }}
                        </el-dropdown-item>
                        <el-dropdown-item command="logout">
                          <el-icon><SwitchButton /></el-icon>
                          退出登录
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </template>
                  </el-dropdown>
                </div>
              </div>
            </el-col>
          </el-row>
        </div>
      </el-header>

      <!-- 主内容区 -->
      <el-main :class="['main-content', { 'no-navigation': !showNavigation }]">
        <router-view />
      </el-main>

      <!-- 底部 - 只在前台页面显示 -->
      <el-footer v-if="showNavigation" class="footer">
        <div class="footer-container">
          <el-row :gutter="40">
            <el-col :xs="24" :sm="8" :md="6">
              <div class="footer-section">
                <h3>🍽️ 美食网</h3>
                <p>发现美食，分享快乐</p>
                <p>让每一餐都成为美好回忆</p>
              </div>
            </el-col>
            <el-col :xs="12" :sm="8" :md="6">
              <div class="footer-section">
                <h3>快速导航</h3>
                <div class="footer-links">
                  <router-link to="/">首页</router-link>
                  <router-link to="/popular">热门推荐</router-link>
                  <router-link to="/categories">美食分类</router-link>
                  <router-link to="/restaurants">美食商家</router-link>
                </div>
              </div>
            </el-col>
            <el-col :xs="12" :sm="8" :md="6">
              <div class="footer-section">
                <h3>联系我们</h3>
                <div class="contact-info">
                  <p><el-icon><Message /></el-icon> contact@meishi.com</p>
                  <p><el-icon><Phone /></el-icon> 400-123-4567</p>
                  <p><el-icon><Location /></el-icon> 北京市朝阳区</p>
                </div>
              </div>
            </el-col>
            <el-col :xs="24" :sm="24" :md="6">
              <div class="footer-section">
                <h3>关注我们</h3>
                <div class="social-links">
                  <div class="social-item">
                    <el-icon><ChatDotRound /></el-icon>
                    <span>微信公众号: 美食网</span>
                  </div>
                  <div class="social-item">
                    <el-icon><Star /></el-icon>
                    <span>微博: @美食网官方</span>
                  </div>
                </div>
              </div>
            </el-col>
          </el-row>
        </div>
      </el-footer>
    </el-container>
  </div>
</template>

<script>
import { ArrowDown, Search, User, SwitchButton, Message, Phone, Location, ChatDotRound, Star } from '@element-plus/icons-vue'

export default {
  name: 'App',
  components: {
    ArrowDown,
    Search,
    User,
    SwitchButton,
    Message,
    Phone,
    Location,
    ChatDotRound,
    Star
  },
  data() {
    return {
      // 添加响应式数据来跟踪登录状态
      loginStatus: false,
      userInfo: null
    }
  },
  computed: {
    isLoggedIn() {
      return this.loginStatus && !!localStorage.getItem('token')
    },
    currentUser() {
      return this.userInfo || {
        username: localStorage.getItem('username'),
        role: localStorage.getItem('userRole')
      }
    },
    showNavigation() {
      const currentPath = this.$route.path
      
      // 需要隐藏导航栏的页面
      if (currentPath === '/login' || currentPath === '/register') {
        return false
      }
      
      // 隐藏管理员后台
      if (currentPath.startsWith('/admin')) {
        return false
      }
      
      // 隐藏用户中心
      if (currentPath.startsWith('/user')) {
        return false
      }
      
      // 隐藏商家后台，但显示商家详情页面（/merchant/数字）
      if (currentPath.startsWith('/merchant/')) {
        // 如果是纯数字，则是商家详情页面，应该显示导航栏
        const merchantIdMatch = currentPath.match(/^\/merchant\/(\d+)$/)
        if (merchantIdMatch) {
          return true  // 商家详情页面显示导航栏
        }
        return false  // 其他商家相关页面（如dashboard）隐藏导航栏
      }
      
      return true
    }
  },
  methods: {
    handleMenuSelect(index) {
      this.$router.push(index)
    },
    getDashboardRoute() {
      const userRole = localStorage.getItem('userRole')
      switch(userRole) {
        case 'admin':
          return '/admin/dashboard'
        case 'merchant':
          return '/merchant/dashboard'
        default:
          return '/user/dashboard'
      }
    },
    handleUserCommand(command) {
      if (command === 'logout') {
        this.logout()
      } else {
        this.$router.push(command)
      }
    },
    logout() {
      this.$confirm('确定要退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        localStorage.removeItem('token')
        localStorage.removeItem('userRole')
        localStorage.removeItem('username')
        localStorage.removeItem('userId')
        localStorage.removeItem('adminId')
        localStorage.removeItem('merchantId')
        
        // 更新响应式数据
        this.loginStatus = false
        this.userInfo = null
        
        this.$router.push('/')
        this.$message.success('退出登录成功')
      }).catch(() => {
        // 用户取消退出
      })
    },
    getUserAvatar() {
      const userRole = localStorage.getItem('userRole')
      switch(userRole) {
        case 'admin':
          return 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png' // 管理员头像
        case 'merchant':
          return '/businessmen.png' // 商家头像
        case 'user':
        default:
          return 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png' // 用户头像
      }
    },
    getDropdownText() {
      const userRole = localStorage.getItem('userRole')
      switch(userRole) {
        case 'admin':
          return '前往后台'
        case 'merchant':
          return '商家后台'
        case 'user':
        default:
          return '个人中心'
      }
    },
    // 更新登录状态的方法
    updateLoginStatus() {
      const token = localStorage.getItem('token')
      const username = localStorage.getItem('username')
      const userRole = localStorage.getItem('userRole')
      
      this.loginStatus = !!token
      this.userInfo = token && username ? { username, role: userRole } : null
    },
    handleStorageChange(e) {
      if (e.key === 'token' || e.key === 'username' || e.key === 'userRole') {
        this.updateLoginStatus()
      }
    }
  },
  // 添加生命周期钩子和监听器
  mounted() {
    // 初始化登录状态
    this.updateLoginStatus()
    
    // 监听localStorage变化
    window.addEventListener('storage', this.handleStorageChange)
    
    // 监听路由变化，用于检测登录状态变化
    this.$watch('$route', () => {
      this.updateLoginStatus()
    })
  },
  beforeUnmount() {
    window.removeEventListener('storage', this.handleStorageChange)
  }
}
</script>

<style>
#app {
  font-family: 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: var(--text-primary);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.el-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* 头部导航栏 */
.header {
  background: var(--background-primary);
  box-shadow: var(--shadow-md);
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 1000;
  border-bottom: 1px solid var(--border-light);
  height: 70px !important;
  padding: 0 !important;
}

.header-container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 var(--spacing-lg);
  height: 100%;
}

.logo {
  font-size: 1.5rem;
  font-weight: 800;
  background: var(--gradient-primary);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  cursor: pointer;
  transition: all 0.3s ease;
  white-space: nowrap;
}

.logo:hover {
  transform: scale(1.05);
}

.nav-menu {
  border-bottom: none !important;
  background: transparent !important;
  height: 70px !important;
  line-height: 70px !important;
}

.el-menu--horizontal > .el-menu-item {
  border-bottom: 3px solid transparent !important;
  color: var(--text-secondary) !important;
  font-weight: 500;
  transition: all 0.3s ease !important;
  height: 70px !important;
  line-height: 70px !important;
}

.el-menu--horizontal > .el-menu-item:hover,
.el-menu--horizontal > .el-menu-item.is-active {
  color: var(--primary-color) !important;
  border-bottom-color: var(--primary-color) !important;
  background: rgba(59, 130, 246, 0.05) !important;
}

/* 头部右侧布局 */
.header-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  height: 100%;
}

.user-actions {
  width: 100%;
  display: flex;
  justify-content: flex-end;
}

.auth-buttons {
  display: flex;
  gap: 8px;
  align-items: center;
}

.auth-buttons .el-button {
  border-radius: var(--radius-lg) !important;
  font-weight: 500;
  padding: 8px 16px !important;
  transition: all 0.3s ease !important;
}

.auth-buttons .el-button--primary {
  background: var(--gradient-primary) !important;
  border: none !important;
}

.auth-buttons .el-button--primary:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-lg) !important;
}

.auth-buttons .el-button:not(.el-button--primary) {
  border: 2px solid var(--border-color) !important;
  color: var(--text-primary) !important;
  background: transparent !important;
}

.auth-buttons .el-button:not(.el-button--primary):hover {
  border-color: var(--primary-color) !important;
  color: var(--primary-color) !important;
  background: rgba(59, 130, 246, 0.05) !important;
}

.user-dropdown {
  display: flex;
  justify-content: flex-end;
}

.user-info {
  cursor: pointer;
  color: var(--primary-color);
  font-weight: 500;
  padding: 8px 12px;
  border-radius: var(--radius-lg);
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
}

.user-info:hover {
  background: rgba(59, 130, 246, 0.1);
}

.username {
  font-size: 0.9rem;
}

/* 主内容区 */
.main-content {
  flex: 1;
  padding: 0 !important;
  background: var(--background-secondary);
}

.main-content.no-navigation {
  min-height: 100vh;
}

/* 底部 */
.footer {
  background: var(--text-primary);
  color: white;
  padding: 40px 0 20px;
  position: relative;
  overflow: visible;
  margin-top: auto;
  min-height: 280px;
}

.footer::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: var(--gradient-accent);
}

.footer-container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 var(--spacing-lg);
}

.footer-section {
  margin-bottom: var(--spacing-lg);
}

.footer h3 {
  color: white !important;
  margin-bottom: 16px !important;
  font-size: 1.2rem !important;
  font-weight: 600 !important;
  line-height: 1.4 !important;
}

.footer p {
  color: rgba(255, 255, 255, 0.8) !important;
  margin: 0 0 8px 0 !important;
  line-height: 1.6 !important;
  font-size: 0.9rem;
}

.footer-links {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.footer-links a {
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  font-size: 0.9rem;
  transition: color 0.3s ease;
  line-height: 1.5;
}

.footer-links a:hover {
  color: var(--secondary-color);
}

.contact-info p,
.social-item {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px !important;
  line-height: 1.5;
}

.social-links {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.copyright {
  color: rgba(255, 255, 255, 0.6);
  margin-top: var(--spacing-lg);
  padding-top: var(--spacing-lg);
}

.copyright p {
  margin: 0 !important;
  font-size: 0.85rem !important;
}

.footer-extra {
  text-align: right;
  font-size: 0.85rem;
  color: rgba(255, 255, 255, 0.6);
}

.el-divider--horizontal {
  background: rgba(255, 255, 255, 0.1) !important;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .header-container {
    padding: 0 var(--spacing-md);
  }
}

@media (max-width: 768px) {
  .logo {
    font-size: 1.3rem;
  }
  
  .header-right {
    gap: 8px;
  }
  
  .search-container {
    display: none;
  }
  
  .auth-buttons {
    gap: 6px;
  }
  
  .auth-buttons .el-button {
    padding: 6px 12px !important;
    font-size: 0.85rem !important;
  }
  
  .username {
    display: none;
  }
  
  .footer {
    min-height: auto;
    padding: 30px 0 15px;
  }
  
  .footer-section {
    margin-bottom: 20px;
  }
  
  .footer-extra {
    text-align: left;
    margin-top: 15px;
  }
}

@media (max-width: 576px) {
  .header-container {
    padding: 0 var(--spacing-sm);
  }
  
  .logo {
    font-size: 1.2rem;
  }
  
  .footer-container {
    padding: 0 var(--spacing-sm);
  }
}
</style>
